# -*- coding: utf-8 -*-
# @Time    : 2019/10/7 12:33
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : search_indexes.py

# 建立索引 索引字段：客户端搜索关键字可能包含的内容

import datetime
from haystack import indexes
from web_ask.news.models import News
from web_ask.articles.models import Article
from web_ask.qa.models import Question
from django.contrib.auth import get_user_model
from taggit.models import Tag  # 标签


class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
    """对Article模型类中部分字段上建立索引,use_template=True使用"""
    text = indexes.CharField(document=True, use_template=True, template_name="search/articles_text.txt")

    def get_model(self):
        return Article

    def index_queryset(self, using=None):
        """当Article模型类中的索引有更新的时候调用, 检索已发表的文章"""
        return self.get_model().objects.filter(status="P", updated_at__lte=datetime.datetime.now())


class NewsIndex(indexes.SearchIndex, indexes.Indexable):
    """对News模型类中部分字段上建立索引,use_template=True使用"""
    text = indexes.CharField(document=True, use_template=True, template_name="search/news_text.txt")

    def get_model(self):
        return News

    def index_queryset(self, using=None):
        """当News模型类中的索引有更新的时候调用, 检索已发表的文章"""
        return self.get_model().objects.filter(reply=False, updated_at__lte=datetime.datetime.now())


class QuestionIndex(indexes.SearchIndex, indexes.Indexable):
    """对Question模型类中部分字段建立索引"""
    text = indexes.CharField(document=True, use_template=True, template_name='search/questions_text.txt')

    def get_model(self):
        return Question

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(updated_at__lte=datetime.datetime.now())


class UserIndex(indexes.SearchIndex, indexes.Indexable):
    """对User模型类中部分字段建立索引"""
    text = indexes.CharField(document=True, use_template=True, template_name='search/users_text.txt')

    def get_model(self):
        return get_user_model()

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(updated_at__lte=datetime.datetime.now())


class TagsIndex(indexes.SearchIndex, indexes.Indexable):
    """对Tags模型类中部分字段建立索引"""
    text = indexes.CharField(document=True, use_template=True, template_name='search/tags_text.txt')

    def get_model(self):
        return Tag

    def index_queryset(self, using=None):
        return self.get_model().objects.all()
